package com.karolak.math.view.tmp;

import java.awt.GridLayout;
import java.util.Map.Entry;

import org.apache.commons.math3.complex.Complex;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import com.karolak.data.DataProvider;
import com.karolak.math.MathUtils;
import com.karolak.math.formula.Fft;

public class PlotFrame extends ApplicationFrame {

	private static final long serialVersionUID = 1L;

	private int chartCount;

//	private Map<Object, >
	
	public PlotFrame(String title) {
		super(title);
		runFft();

		setLayout(new GridLayout(getChartCount(), 1));
		pack();
		setVisible(true);
	}

	public static void main(String[] args) {
		RefineryUtilities.centerFrameOnScreen(new PlotFrame("hw"));
	}

	public void runFft() {
		// double[] x = DataProvider.getX();
		// double[] y = DataProvider.getY(x);
		//double[] x = DataProvider.getX(512);
		//double[] y = DataProvider.getFileData(1024);
		
		DataProvider provider = new DataProvider();
		
//		double[] x = provider.getData();
		double[] y = provider.getData();

		int harmonicCount = 5;

		double[] sample = y;
		
		Fft fft = new Fft();
		fft.calculate(sample, harmonicCount);
		
//		Complex[] spectrum = TransUtils.fft(sample);
//		Complex[] invertedSample = TransUtils.inFft(spectrum);
//		double[] filtered = TransUtils.filter(sample, 5);
		//double[][] harmonics = TransUtils.getHarmonics(spectrum, harmonicCount);

//		add(createPanel(MathUtils.to2D(sample)));
//		add(createPanel(MathUtils.to2D(MathUtils.toAbs(spectrum))));
//		add(createPanel(MathUtils.to2D(MathUtils.toReal(invertedSample))));
//		add(createPanel(MathUtils.to2D(filtered)));


		add(createPanel(MathUtils.to2D(sample)));
		add(createPanel(MathUtils.to2D(fft.getFiltered())));
//		add(createPanel(MathUtils.to2D(Fft.calculate2(sample, harmonicCount))));
		
		add(createPanel(MathUtils.to2D(MathUtils.toAbs(fft.getSpectrum()))));

		for (int fr  : fft.getHarmonics().keySet()) {
			add(createPanel(MathUtils.to2D(fft.getHarmonics().get(fr))));
		}

		for (int ix = 0; ix < fft.getSignificantFrequencies().size(); ix++) {
			Entry<Integer, Complex> entry = fft.getSignificantFrequencies().get(ix);
			double max = MathUtils.getMax(fft.getHarmonics().get(entry.getKey()));
			System.out.println(fft.describeHarmonic(ix));

		}

//		add(createPanel(compl));
		for (int i = 0; i < harmonicCount; i++) {
		//	 add(createPanel(MathUtils.to2D(harmonics[i])));

		}
	}

	protected void updateChart(int chartId, double x, double y) {

	}

	protected ChartPanel createPanel(double[][] input) {
		XYDataset data = createDataSet(input);
		XYPlot plot = new XYPlot(data, new NumberAxis(), new NumberAxis(), new StandardXYItemRenderer());
		JFreeChart chart = new JFreeChart("hw", plot);
		ChartPanel panel = new ChartPanel(chart);
		setChartCount(getChartCount() + 1);


		
		double x0 = input[0][0];
		double xn = input[0][input[0].length - 1];

		double yMin = MathUtils.getMin(input[1]);
		double yMax = MathUtils.getMax(input[1]);

		plot.getDomainAxis().setRange(x0, xn);
		plot.getRangeAxis().setRange(yMin, yMax);

		return panel;
	}

	protected XYDataset createDataSet(double[][] input) {
		DefaultXYDataset ds = new DefaultXYDataset();
		ds.addSeries(new Comparable<String>() {
			public int compareTo(String o) {
				return 1;
			};
		}, input);
		return ds;
	}

	protected int getChartCount() {
		return chartCount;
	}

	protected void setChartCount(int chartCount) {
		this.chartCount = chartCount;
	}
}
